/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;

/**
 *
 * @author Administrator
 */
public class MenuCanvas extends GameCanvas implements Runnable, PlayerListener {

    //midlet
    private BTIMidlet midlet;
    //---------------------------------------------------
    //sound
    private int x = 0;
    private int y = 0;
    private int xLoading = 0;
    private int yLoading = 0;
    //---------------------------------------------------
    //image
    private Image imgObjekWisataOn;
    private Image imgObjekWisataOff;
    private String pathOBjekWisataOn;
    private String pathOBjekWisataOff;
    private int xObjekWisata = 0;
    private int yObjekWisata = 0;
    private Image imgHelpOn;
    private Image imgHelpOff;
    private String pathHelpOn;
    private String pathHelpOff;
    private int xHelp = 0;
    private int yHelp = 0;
    private Image imgHotelOn;
    private Image imgHotelOff;
    private String pathHotelOn;
    private String pathHotelOff;
    private int xHotel = 0;
    private int yHotel = 0;
    private Image imgKulinerOn;
    private Image imgKulinerOff;
    private String pathKulinerOn;
    private String pathKulinerOff;
    private int xKuliner = 0;
    private int yKuliner = 0;
    private Image imgPetaOn;
    private Image imgPetaOff;
    private String pathPetaOn;
    private String pathPetaOff;
    private int xPeta = 0;
    private int yPeta = 0;
    private Image imgPilihanOn;
    private Image imgPilihanOff;
    private String pathPilihanOn;
    private String pathPilihanOff;
    private int xPilihan = 0;
    private int yPilihan = 0;
    private Image imgExitOn;
    private Image imgExitOff;
    private String pathExitOn;
    private String pathExitOff;
    private int xExit = 0;
    private int yExit = 0;
    //animated sprite
    private Sprite spLoading;
    private boolean running = false;
    private LayerManager lmgr;
    //---------------------------------------------------
    //speed 
    private int speedGame = 200;
    private int xMenu = 0;
    private int xJarak = 0;
    private int kursorMenu = 0;
    //---------------------------------------------------
    //sound
    private long delay;
    private Player sound01;
    private String statusKey;

    public MenuCanvas(BTIMidlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;
        /*
         * status kursorMenu
         *  1 - Objek Wisata
        2 - Tempat Kuliner
        3 - Hotel
        4 - Peta
        5 - Pilihan
        6 - Bantuan
        7 - Keluar
         */

        if (BTIMidlet.WIDTH == 128) {
            pathOBjekWisataOff = "/icon_objek_wisata_off_128.png";
            pathOBjekWisataOn = "/icon_objek_wisata_on_128.png";

            pathKulinerOff = "/icon_kuliner_off_128.png";
            pathKulinerOn = "/icon_kuliner_on_128.png";

            pathHotelOff = "/icon_hotel_off_128.png";
            pathHotelOn = "/icon_hotel_on_128.png";

            pathPetaOff = "/icon_peta_off_128.png";
            pathPetaOn = "/icon_peta_on_128.png";

            pathPilihanOff = "/icon_pilihan_off_128.png";
            pathPilihanOn = "/icon_pilihan_on_128.png";

            pathHelpOff = "/icon_help_off_128.png";
            pathHelpOn = "/icon_help_on_128.png";

            pathExitOff = "/icon_exit_off_128.png";
            pathExitOn = "/icon_exit_on_128.png";

        } else if (BTIMidlet.WIDTH == 240) {
            pathOBjekWisataOff = "/icon_objek_wisata_off_240.png";
            pathOBjekWisataOn = "/icon_objek_wisata_on_240.png";

            pathKulinerOff = "/icon_kuliner_off_240.png";
            pathKulinerOn = "/icon_kuliner_on_240.png";

            pathHotelOff = "/icon_hotel_off_240.png";
            pathHotelOn = "/icon_hotel_on_240.png";

            pathPetaOff = "/icon_peta_off_240.png";
            pathPetaOn = "/icon_peta_on_240.png";

            pathPilihanOff = "/icon_pilihan_off_240.png";
            pathPilihanOn = "/icon_pilihan_on_240.png";

            pathHelpOff = "/icon_help_off_240.png";
            pathHelpOn = "/icon_help_on_240.png";

            pathExitOff = "/icon_exit_off_240.png";
            pathExitOn = "/icon_exit_on_240.png";
        }

        xMenu = (int) (getHeight() * 0.07);

        xObjekWisata = (int) (BTIMidlet.WIDTH * 0);
        yObjekWisata = (int) (BTIMidlet.HEIGHT * 0.1);

        xKuliner = (int) (BTIMidlet.WIDTH * 0.6);
        yKuliner = (int) (BTIMidlet.HEIGHT * 0.1);

        xHotel = (int) (BTIMidlet.WIDTH * 0);
        yHotel = (int) (BTIMidlet.HEIGHT * 0.4);

        xPeta = (int) (BTIMidlet.WIDTH * 0.3);
        yPeta = (int) (BTIMidlet.HEIGHT * 0.4);

        xPilihan = (int) (BTIMidlet.WIDTH * 0.6);
        yPilihan = (int) (BTIMidlet.HEIGHT * 0.4);

        xHelp = (int) (BTIMidlet.WIDTH * 0);
        yHelp = (int) (BTIMidlet.HEIGHT * 0.7);

        xExit = (int) (BTIMidlet.WIDTH * 0.6);
        yExit = (int) (BTIMidlet.HEIGHT * 0.7);

        kursorMenu = 1;
        try {
            imgObjekWisataOff = Image.createImage(pathOBjekWisataOff);
            imgObjekWisataOn = Image.createImage(pathOBjekWisataOn);

            imgKulinerOff = Image.createImage(pathKulinerOff);
            imgKulinerOn = Image.createImage(pathKulinerOn);

            imgHotelOff = Image.createImage(pathHotelOff);
            imgHotelOn = Image.createImage(pathHotelOn);

            imgPetaOff = Image.createImage(pathPetaOff);
            imgPetaOn = Image.createImage(pathPetaOn);

            imgPilihanOff = Image.createImage(pathPilihanOff);
            imgPilihanOn = Image.createImage(pathPilihanOn);

            imgHelpOff = Image.createImage(pathHelpOff);
            imgHelpOn = Image.createImage(pathHelpOn);

            imgExitOff = Image.createImage(pathExitOff);
            imgExitOn = Image.createImage(pathExitOn);

            spLoading = new Sprite(Image.createImage("/loading.png"),
                    50, 50);
            xLoading = xKuliner;
            yLoading = yKuliner / 2;
            spLoading.setPosition(xLoading, yLoading);

            lmgr = new LayerManager();
            lmgr.append(spLoading);
        } catch (Exception e) {
        }

        xJarak = 30;

        try {
            sound01 = Manager.createPlayer(getClass().getResourceAsStream("/urban_01.mid"), "audio/midi");
            sound01.realize();
            sound01.prefetch();
            sound01.addPlayerListener(this);
            sound01.setLoopCount(-1);
            sound01.start();
        } catch (Exception e) {
        }
    }

    //start thread
    public void start() {
        running = true;
        Thread t = new Thread(this);
        t.start();
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            drawDisplay(g);
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
        try {
            sound01.stop();
        } catch (MediaException ex) {
        }
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());

        g.setColor(0x0000ff);
        if (BTIMidlet.WIDTH == 128) {
            g.drawRect(0, 0, 128, 170);
        }

        switch (kursorMenu) {
            case 1:
                g.drawImage(imgObjekWisataOn, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 2:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOn, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 3:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOn, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 4:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOn, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 5:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOn, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 6:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOn, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOff, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
            case 7:
                g.drawImage(imgObjekWisataOff, xObjekWisata, yObjekWisata, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgKulinerOff, xKuliner, yKuliner, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHotelOff, xHotel, yHotel, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPetaOff, xPeta, yPeta, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgPilihanOff, xPilihan, yPilihan, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgHelpOff, xHelp, yHelp, Graphics.TOP | Graphics.LEFT);
                g.drawImage(imgExitOn, xExit, yExit, Graphics.TOP | Graphics.LEFT);
                break;
        }
        

        spLoading.nextFrame();
        spLoading.setPosition(xLoading, yLoading);

        //paint all layers
        lmgr.paint(g, 0, 0);
        flushGraphics();
    }

    protected void keyPressed(int keyCode) {
        //statusKey = String.valueOf(keyCode);
        //if (MathackMIDlet.LANDSCAPE == 1) { //landscape
        if (keyCode == -5) {
            if (kursorMenu == 1) {
                BTIMidlet.BEFORE = 1;
                midlet.showObjekWisataList();
            }
            if (kursorMenu == 3) {
                BTIMidlet.BEFORE = 2;
                midlet.showTempatKuliner();
            }
            if (kursorMenu == 3) {
                BTIMidlet.BEFORE = 3;
                midlet.showHotelList();
            }
            if (kursorMenu == 4) {
                BTIMidlet.BEFORE = 4;
                midlet.showPeta();
            }
            if (kursorMenu == 5) {
                BTIMidlet.BEFORE = 5;
                midlet.showPilihan();
            }
            if (kursorMenu == 6) {
                BTIMidlet.BEFORE = 6;
                midlet.showHelp();
            }
            if (kursorMenu == 7) {
                BTIMidlet.BEFORE = 7;
                midlet.exit();
            }
        } else if (keyCode == -2) { //down
            if (kursorMenu == 7) { //down
                kursorMenu = 0;
            }
            kursorMenu++;

        } else if (keyCode == -4) { //right
        } else if (keyCode == -1) { //up
            if (kursorMenu == 1) { //up
                kursorMenu = 8;
            }
            kursorMenu--;
        } else if (keyCode == -3) { //left
        } else if (keyCode == -7) {
            midlet.exit();
        } else if (keyCode == -6) {
        } else if (keyCode == 50) { //LA-UP 2
        } else if (keyCode == 52) { //LA-LEFT 4
        } else if (keyCode == 53) { //LA-CENTER 5
            if (kursorMenu == 1) {
                //midlet.showMainGame();
            }
            if (kursorMenu == 7) {
                midlet.exit();
            }
        } else if (keyCode == 54) { //LA-RIGHT 6
        } else if (keyCode == 56) { //LA-DOWN 8
        } else if (keyCode == 103) { //QW-nofire-CENTER 5
            if (kursorMenu == 1) {
                //midlet.showMainGame();
            }
            if (kursorMenu == 7) {
                midlet.exit();
            }
        }
        repaint();
    }

    public void playerUpdate(Player arg0, String eventType, Object arg2) {
        if (eventType.equals(STARTED)) {
            delay = System.currentTimeMillis() - delay;
            repaint();
        } else if (eventType.equals(END_OF_MEDIA)) {
            delay = System.currentTimeMillis();
        }
    }
}
